<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BaseController extends Controller
{
    /**
     * 验证第一个失败时就返回
     * @param Request $request
     * @param array<string, mixed> $rules
     * @param array<string, mixed> $messages
     * @return string|null
     */
    public function validatorStopOnFirst(Request $request, array $rules, array $messages = []): ?string
    {
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->stopOnFirstFailure()->fails()) {
            $errors = $validator->errors();
            return $errors->first();
        }
        return null;
    }
}
